package com.example.entreamigoslocal;

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListarEventoActivity extends Activity {

	BdCadastro dbHelper = null;
	SQLiteDatabase db = null;
	
	ArrayAdapter<String> eventosAdapter = null;
	
	protected void atualizarLista(){
		db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("SELECT NM_EVENTO, TP_EVENTO, DT_EVENTO FROM EVENTO", null);
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			String nome = cursor.getString(0);
			String tipo = cursor.getString(1);
			String data = cursor.getString(2);
			eventosAdapter.add(nome + " - " + tipo + " - " + data);
			
			cursor.moveToNext();
		}
		cursor.close();
		db.close();
		dbHelper.close();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_listar_evento);
		
		  ArrayList<String> eventos = new ArrayList<String>();
	        eventosAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, eventos);
	        ListView listViewEventos = (ListView) findViewById(R.id.listView1);
	        listViewEventos.setAdapter(eventosAdapter);
	        try{
	        	dbHelper = new BdCadastro(getApplicationContext());
	        	atualizarLista();
	        }catch(Exception ex){
	        	new AlertDialog.Builder(this).setMessage(ex.getMessage()).setPositiveButton("OK", null).show();
	        }
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.listar_evento, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
